home *** CD-ROM | disk | FTP | other *** search
/ Champak 106 / Vol 106.iso / games / global_r.swf / scripts / __Packages / Scrollbar.as < prev    next >
Encoding:
Text File  |  2010-04-12  |  7.0 KB  |  227 lines

  1. class Scrollbar extends MovieClip
  2. {
  3.    var scrollBarTrackTop;
  4.    var up;
  5.    var scrollBarTrackHeight;
  6.    var lowestYPosForBar;
  7.    var onMouseUp;
  8.    var down;
  9.    var middle;
  10.    var bg;
  11.    var onEnterFrame;
  12.    var scrollButtonPressed = false;
  13.    var scrollerMovingBar = 0;
  14.    var scrollerMovingBarMove = 10;
  15.    var scrollerMovingBarJump = 90;
  16.    var scrollBarPercentage = 0;
  17.    var scrollerDragging = false;
  18.    var scrollerDragOffset = 0;
  19.    var scrollBarTopOfWindow = 0;
  20.    var scrollBarWindowShowHeight = 0;
  21.    var scrollBarWindowScrollHeight = 0;
  22.    var scrollerAttachedTo = null;
  23.    function Scrollbar()
  24.    {
  25.       super();
  26.    }
  27.    function attachScrollBar(attachTo, bottomMargin)
  28.    {
  29.       this.scrollBarTrackTop = this.up._y + this.up._height + 2;
  30.       this.scrollBarTrackHeight = this._height - this.scrollBarTrackTop * 2;
  31.       this.lowestYPosForBar = 0;
  32.       this.scrollBarTopOfWindow = attachTo._y;
  33.       var _loc4_ = this._height;
  34.       if(attachTo._height > _loc4_)
  35.       {
  36.          this.scrollerAttachedTo = attachTo;
  37.          if(attachTo.selectClass)
  38.          {
  39.             attachTo.selectClass.attachedScrollBar = this;
  40.          }
  41.          else
  42.          {
  43.             attachTo.attachedScrollBar = this;
  44.          }
  45.          this.scrollBarWindowShowHeight = _loc4_;
  46.          this._visible = true;
  47.          this.onMouseUp = this.stopScroll;
  48.          this.up.onPress = function()
  49.          {
  50.             this._parent.startScrollUp();
  51.          };
  52.          this.down.onPress = function()
  53.          {
  54.             this._parent.startScrollDown();
  55.          };
  56.          this.middle.onPress = function()
  57.          {
  58.             this._parent.startScrollDrag();
  59.          };
  60.          this.bg.onPress = function()
  61.          {
  62.             this._parent.scrollerBGPressed();
  63.          };
  64.          this.scrollBarWindowScrollHeight = attachTo._height + bottomMargin;
  65.          this.checkScrollingWindow();
  66.          var _loc2_ = this.scrollBarTrackHeight * (this.scrollBarWindowShowHeight / this.scrollBarWindowScrollHeight);
  67.          if(_loc2_ > this.scrollBarTrackHeight)
  68.          {
  69.             _loc2_ = this.scrollBarTrackHeight;
  70.          }
  71.          if(_loc2_ < 10)
  72.          {
  73.             _loc2_ = 10;
  74.          }
  75.          this.middle._height = _loc2_;
  76.          this.middle._y = this.scrollBarTrackTop;
  77.          this.lowestYPosForBar = this.scrollBarTrackHeight - this.middle._height;
  78.          this.moveScrollBarPosition();
  79.       }
  80.       else
  81.       {
  82.          this.removeScrollBar();
  83.       }
  84.    }
  85.    function removeScrollBar(Void)
  86.    {
  87.       this._visible = false;
  88.       if(this.scrollerAttachedTo)
  89.       {
  90.          this.scrollerAttachedTo._y = this.scrollBarTopOfWindow;
  91.       }
  92.       this.scrollerAttachedTo = null;
  93.       this.onMouseUp = null;
  94.       this.up.onPress = null;
  95.       this.down.onPress = null;
  96.       this.middle.onPress = null;
  97.       this.bg.onPress = null;
  98.    }
  99.    function moveScroller(Void)
  100.    {
  101.       if(this.scrollerDragging)
  102.       {
  103.          var _loc2_ = this.getMouseYRelativeTo(this) - this.scrollerDragOffset;
  104.          if(_loc2_ < 0)
  105.          {
  106.             _loc2_ = 0;
  107.          }
  108.          if(_loc2_ > this.lowestYPosForBar)
  109.          {
  110.             _loc2_ = this.lowestYPosForBar;
  111.          }
  112.          this.scrollerAttachedTo._y = this.scrollBarTopOfWindow - _loc2_ / this.lowestYPosForBar * (this.scrollBarWindowScrollHeight - this.scrollBarWindowShowHeight);
  113.          this.checkScrollingWindow();
  114.          this.moveScrollBarPosition();
  115.       }
  116.       else if(this.scrollerMovingBar != 0)
  117.       {
  118.          this.scrollerAttachedTo._y += this.scrollerMovingBar;
  119.          this.checkScrollingWindow();
  120.          this.moveScrollBarPosition();
  121.       }
  122.       if(!this.scrollerDragging && this.scrollerMovingBar != 0)
  123.       {
  124.          if(!this.scrollButtonPressed)
  125.          {
  126.             this.scrollerMovingBar *= 0.6;
  127.             if(this.scrollerMovingBar > 0 && this.scrollerMovingBar < 1)
  128.             {
  129.                this.scrollerMovingBar = 0;
  130.                this.onEnterFrame = null;
  131.             }
  132.             else if(this.scrollerMovingBar < 0 && this.scrollerMovingBar > -1)
  133.             {
  134.                this.scrollerMovingBar = 0;
  135.                this.onEnterFrame = null;
  136.             }
  137.          }
  138.          else
  139.          {
  140.             this.scrollerMovingBar *= 1.1;
  141.          }
  142.       }
  143.    }
  144.    function scrollerBGPressed(Void)
  145.    {
  146.       var _loc2_ = this.getMouseYRelativeTo(this) - this.middle._y;
  147.       if(_loc2_ < 0)
  148.       {
  149.          this.scrollerMovingBar = this.scrollerMovingBarJump;
  150.       }
  151.       if(_loc2_ > this.middle._height)
  152.       {
  153.          this.scrollerMovingBar = - this.scrollerMovingBarJump;
  154.       }
  155.       this.scrollButtonPressed = true;
  156.       this.scrollerDragging = false;
  157.       this.onEnterFrame = this.moveScroller;
  158.    }
  159.    function jumpToPosition(jumpToPos)
  160.    {
  161.       this.scrollerAttachedTo._y = this._y + jumpToPos;
  162.       this.checkScrollingWindow();
  163.       this.moveScrollBarPosition();
  164.    }
  165.    function checkScrollingWindow(Void)
  166.    {
  167.       if(this.scrollerAttachedTo._y < this.scrollBarWindowShowHeight + this.scrollBarTopOfWindow - this.scrollBarWindowScrollHeight)
  168.       {
  169.          this.scrollerAttachedTo._y = this.scrollBarWindowShowHeight + this.scrollBarTopOfWindow - this.scrollBarWindowScrollHeight;
  170.          if(!this.scrollerDragging)
  171.          {
  172.             this.stopScroll();
  173.          }
  174.       }
  175.       if(this.scrollerAttachedTo._y > this.scrollBarTopOfWindow)
  176.       {
  177.          this.scrollerAttachedTo._y = this.scrollBarTopOfWindow;
  178.          if(!this.scrollerDragging)
  179.          {
  180.             this.stopScroll();
  181.          }
  182.       }
  183.    }
  184.    function moveScrollBarPosition(Void)
  185.    {
  186.       if(this.scrollerAttachedTo)
  187.       {
  188.          this.middle._y = this.scrollBarTrackTop + Math.abs(this.scrollerAttachedTo._y - this.scrollBarTopOfWindow) / (this.scrollBarWindowScrollHeight - this.scrollBarWindowShowHeight) * this.lowestYPosForBar;
  189.       }
  190.    }
  191.    function startScrollUp(Void)
  192.    {
  193.       this.scrollerMovingBar = this.scrollerMovingBarMove;
  194.       this.scrollButtonPressed = true;
  195.       this.scrollerDragging = false;
  196.       this.onEnterFrame = this.moveScroller;
  197.    }
  198.    function startScrollDown(Void)
  199.    {
  200.       this.scrollerMovingBar = - this.scrollerMovingBarMove;
  201.       this.scrollButtonPressed = true;
  202.       this.scrollerDragging = false;
  203.       this.onEnterFrame = this.moveScroller;
  204.    }
  205.    function startScrollDrag(Void)
  206.    {
  207.       this.scrollerMovingBar = 0;
  208.       this.scrollerDragOffset = this.getMouseYRelativeTo(this) - this.middle._y + this.scrollBarTrackTop;
  209.       this.scrollerDragging = true;
  210.       this.scrollButtonPressed = false;
  211.       this.onEnterFrame = this.moveScroller;
  212.    }
  213.    function stopScroll(Void)
  214.    {
  215.       this.scrollButtonPressed = false;
  216.       this.scrollerDragging = false;
  217.    }
  218.    function getMouseYRelativeTo(relativeTo)
  219.    {
  220.       var _loc2_ = new Object();
  221.       _loc2_.x = 0;
  222.       _loc2_.y = _root._ymouse;
  223.       relativeTo.globalToLocal(_loc2_);
  224.       return _loc2_.y;
  225.    }
  226. }
  227.